package wendyeq.iweb.blog.dao.impl;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import wendyeq.iweb.blog.dao.ITagsDAO;
import wendyeq.iweb.blog.pojo.Tags;
import wendyeq.iweb.blog.util.JdoManager;

@Component("tagsDAO")
public class TagsDAO implements ITagsDAO{
	private static final Logger logger = Logger.getLogger(TagsDAO.class);
	
	@Override
	public boolean saveOrUpdate(Tags t) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			manager.makePersistent(t);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally {
			manager.close();
		}
		return true;
	}

	public boolean delete(String id) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Tags tag = manager.getObjectById(Tags.class, id);
			manager.deletePersistent(tag);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally {
			manager.close();
		}
		return true;
	}
/*	
	public void deleteByTag(String t) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Tags tag = this.findByTag(t);
			manager.deletePersistent(tag);
		} catch (Exception e) {
			logger.error(e);
		} finally {
			manager.close();
		}
	}
	*/
	
	@Override
	public Tags findById(String id) {
		PersistenceManager manager = JdoManager.getSession();
		Tags tag = null;
		try {
			tag = manager.getObjectById(Tags.class, id);
		} catch (Exception e) {
			logger.error(e);
		}
		return tag;
	}
	
	@Override
	public List<Tags> findByPage(int min, int max) {
		List<Tags> tags = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Tags.class);
			query.setOrdering("createdTime DESC");
			query.setRange(min, max);
			tags = (List<Tags>) query.execute();
			logger.warn("tag-size: " + tags.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return tags;
	}

	@SuppressWarnings("unchecked")
	public List<Tags> findAll() {
		List<Tags> tags = null;
		PersistenceManager manager = JdoManager.getSession();
		try {
			Query query = manager.newQuery(Tags.class);
			query.setOrdering("createdTime DESC");
			tags = (List<Tags>) query.execute();
			logger.warn("tag-size: " + tags.size());
		} catch (Exception e) {
			logger.error(e);
		}
		return tags;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Tags findByTag(String t) {
		Tags tag = null;
		PersistenceManager manager = JdoManager.getSession();
		Query query = manager.newQuery(Tags.class);
		try {
			query.setFilter("name == t");
			query.declareParameters("String t");
			List<Tags> tags = (List<Tags>) query.execute(t);
			if (!tags.isEmpty()) {
				tag = tags.get(0);
			}
		} catch (Exception e) {
			logger.error(e);
		} finally {
			query.closeAll();
		}
		return tag;
	}

	@Override
	public boolean updateByArticle(String id, String aid) {
		PersistenceManager manager = JdoManager.getSession();
		try {
			Tags tag = manager.getObjectById(Tags.class, id);
			tag.getArticles().add(aid);
			manager.makePersistent(tag);
		} catch (Exception e) {
			logger.error(e);
			return false;
		} finally {
			manager.close();
		}
		return true;
	}
	
}
